package com.bot.backend.service.impl.user.snake;

import com.bot.backend.mapper.SnakeMapper;
import com.bot.backend.pojo.Snake;
import com.bot.backend.pojo.User;
import com.bot.backend.service.impl.utils.UserDetailsImpl;
import com.bot.backend.service.user.snake.UpdateSnakeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class UpdateSnakeServiceImpl implements UpdateSnakeService {

    @Autowired
    private SnakeMapper snakeMapper;

    @Override
    public Map<String, String> updateSnake(Map<String, String> data) {
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken =
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();

        UserDetailsImpl loginUser = (UserDetailsImpl) usernamePasswordAuthenticationToken.getPrincipal();

        User user = loginUser.getUser();

        String snake_id = data.get("id");
        String title = data.get("title");
        String content = data.get("content");
        String description = data.get("description");

        Map<String, String> map = new HashMap<>();

        if (title == null || title.length() == 0) {
            map.put("error_message", "标题不能为空");
            return map;
        }
        if (title.length() > 255) {
            map.put("error_message", "标题长度不能超过255");
            return map;
        }

        if (content == null || content.length() == 0) {
            map.put("error_message", "代码内容不能为空");
            return map;
        }

        if (content != null && content.length() > 10000) {
            map.put("error_message", "代码长度不能超过10000");
            return map;
        }

        if (description == null || description.length() == 0) {
            description = "这个用户很懒，什么也没有说";
        }
        if (description.length() > 255) {
            map.put("error_message", "描述长度不能超过255");
            return map;
        }

        Snake snake = snakeMapper.selectById(snake_id);
        if (snake == null) {
            map.put("error_message", "snake不存在或已被删除");
            return map;
        }

        if (!snake.getUserId().equals(user.getId())) {
            map.put("error_message", "没有权限修改该Snake");
            return map;
        }

        Snake new_snake = new Snake();
        new_snake.setId(snake.getId());
        new_snake.setTitle(title);
        new_snake.setContent(content);
        new_snake.setDescription(description);
        new_snake.setUpdateTime(new Date());
        new_snake.setCreateTime(snake.getCreateTime());
        new_snake.setUserId(user.getId());
        int updateById = snakeMapper.updateById(new_snake);
        if (updateById > 0) map.put("error_message", "success");
        return map;
    }
}
